###########################################
### FUNCTIONS                           ###
###########################################

make_map_individual_donations <- function(data,title) {
  data$incomerel <- data$i_don_ind / data$inh
  data <-
    aggregate(incomerel ~ state, data = data, FUN = mean)
  data_mean <- mean(data$incomerel)
  data$deviation <- (data$incomerel / data_mean - 1) * 100
  data$deviation <- ifelse(data$deviation>200,199,data$deviation)
  sample <- data.frame(
    HASC_1 =    c(
      "DE.BR",
      "DE.BE",
      "DE.BW",
      "DE.BY",
      "DE.HB",
      "DE.HE",
      "DE.HH",
      "DE.MV",
      "DE.NI",
      "DE.NW",
      "DE.RP",
      "DE.SH",
      "DE.SL",
      "DE.SN",
      "DE.ST",
      "DE.TH"
    ),
    clicks = NA
  )
  sample$clicks <- data$deviation
  
  my.data <- readRDS("map.rds")
  sample$HASC_1 <- as.character(sample$HASC_1)
  final <-
    merge(
      x = my.data@data,
      y = sample,
      by = "HASC_1",
      all.y = TRUE
    )
  my.data@data <- data.frame(my.data@data,
                             sample[match(my.data@data[, "HASC_1"],
                                          sample[, "HASC_1"]),])
  plot <-
    spplot(
      my.data,
      zcol = "clicks",
      col.regions = gray(seq(1, 0, by = -0.01)),
      at = seq(-100, 200, 10),
      colorkey = list(labels = list(
        labels = c(
          "-100%",
          "-50%",
          "0%",
          "+50%",
          "+100%",
          "+150%",
          ">+200%"
        )))
    )
  return(plot)
}

make_map_corporate_donations <- function(data,title) {
  data$incomerel <- data$i_don_cor / data$inh
  data <-
    aggregate(incomerel ~ state, data = data, FUN = mean)
  data_mean <- mean(data$incomerel)
  data$deviation <- (data$incomerel / data_mean - 1) * 100
  data$deviation <- ifelse(data$deviation>200,199,data$deviation)
  sample <- data.frame(
    HASC_1 =    c(
      "DE.BR",
      "DE.BE",
      "DE.BW",
      "DE.BY",
      "DE.HB",
      "DE.HE",
      "DE.HH",
      "DE.MV",
      "DE.NI",
      "DE.NW",
      "DE.RP",
      "DE.SH",
      "DE.SL",
      "DE.SN",
      "DE.ST",
      "DE.TH"
    ),
    clicks = NA
  )
  sample$clicks <- data$deviation
  
  my.data <- readRDS("map.rds")
  sample$HASC_1 <- as.character(sample$HASC_1)
  final <-
    merge(
      x = my.data@data,
      y = sample,
      by = "HASC_1",
      all.y = TRUE
    )
  my.data@data <- data.frame(my.data@data,
                             sample[match(my.data@data[, "HASC_1"],
                                          sample[, "HASC_1"]),])
  plot <-
    spplot(
      my.data,
      zcol = "clicks",
      col.regions = gray(seq(1, 0, by = -0.01)),
      at = seq(-100, 200, 10),
      colorkey = list(labels = list(
        labels = c(
          "-100%",
          "-50%",
          "0%",
          "+50%",
          "+100%",
          "+150%",
          ">+200%"
        )))
    )
  return(plot)
}

rescale <- function(x)
  (x - min(x)) / (max(x) - min(x)) * 100



